package com.baosight.sczx.zm.cs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ef.ui.tree.TreeConstants;
import com.baosight.iplat4j.ef.ui.tree.TreeService;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.EngineeringDesign;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.OtherDesign;
import com.baosight.sczx.common.zm.cInterface.cs.RoadWay;
import com.baosight.sczx.common.zm.cInterface.cs.WorkingFace;

/**
 * <p>Title: ServiceZMCS50.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-1 上午11:03:58  张小飞  Create
 *</pre>
 */
public class ServiceZMCS50 extends TreeService {

	/**
	 * 一级节点, 此处不构造, 具体方法看query
	 * <p>Function: getTopNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14上午05:08:05 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List getTopNodes() {
		return null;
	}
	
	/**
	 * 子节点, 此处不构造, 具体方法看query
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS50</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-14上午05:08:37 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List getChildNodes(String arg0) {
		return null;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 自定义一个树的查询方法</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午04:03:14 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo query(EiInfo inInfo) {
		QueryMap queryInfo = QueryMap.getQueryMap(inInfo);
		String _node = (String) queryInfo.get(TreeConstants.TREE_NODE);
		String tableName = (String) queryInfo.get("tableName");
		String[] blocks = inInfo.getBlockIds().split(",");
		for ( int i=0;i<blocks.length;i++)
		{
			if(blocks[i].contains("$")){
				//自定义四棵树的根节点block名称中都包含了$
				_node = blocks[i];
				break;
			}
		}
		List children = new ArrayList();
		if (_node == null) {
			_node = "$faceRoadway";
		}
		if (_node.equals("$faceRoadway")||_node.equals("$unit")||_node.equals("$category")||_node.equals("$specialFund")) {
			children = this.getTopNodes(_node);
		} else {
			children = this.getChildNodes(_node, tableName);
		}
		EiInfo outInfo = new EiInfo();
		EiBlock block = outInfo.addBlock(_node);
		block.setBlockMeta(initMetaData());
		block.setRows(children);
		return outInfo;
	}

	/**
	 * 构造工程查询树形菜单中四棵树的根节点
	 * <p>Function: getTopNodes</p>
	 * <p>Description: 自定义查询树的根节点</p>
	 * <p>return value:List</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午04:04:43 张小飞
	 * </p>
	 *
	 */

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List getTopNodes(String node) {
		List _list = new ArrayList();
		if("$faceRoadway".equals(node)){
			_list = dao.query("ZMCSMine.queryToProjectInfo", null);//获取所有矿井名称作为工作面巷道树的根节点
		} else if("$unit".equals(node)){				
			Map<String, String> topMap = new HashMap<String, String>();
			topMap.put("value", "topUnit");
			topMap.put("display", "施工部门");		
			topMap.put("leaf", "0");
			topMap.put("tableName", "");
			_list.add(topMap);
		} else if("$category".equals(node)){				
			Map<String, String> topMap = new HashMap<String, String>();
			topMap.put("value", "topCategory");
			topMap.put("display", "工程类别");		
			topMap.put("leaf", "0");
			topMap.put("tableName", "");
			_list.add(topMap);
		} else if("$specialFund".equals(node)){				
			Map<String, String> topMap = new HashMap<String, String>();
			topMap.put("value", "topSpecialFund");
			topMap.put("display", "专项资金");		
			topMap.put("leaf", "0");
			topMap.put("tableName", "");
			_list.add(topMap);
		}
		return _list;
	}

	/**
	 * <p>Function: getChildNodes</p>
	 * <p>Description: 自定义查询树的孩子节点</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午05:13:12 张小飞
	 * </p>
	 *
	*/
	@SuppressWarnings({ "rawtypes"})
	public List getChildNodes(String _node, String tableName) {//构造四棵树所有的非根节点
		List<Map> childNodes = new ArrayList<Map>();

		if("topUnit".equals(_node)){
			//二级, 生产单位树的叶子节点
			childNodes = Engineering.getDepartToProjectInfo();
		} else if("topCategory".equals(_node)){
			//二级, 类别树的叶子节点	
			childNodes = this.getTreeNodesFromInitialData();
		} else if("topSpecialFund".equals(_node)){
			//二级, 专项资金树的叶子节点	
			childNodes = this.getTreeNodesFromInitialData("cmes.cs.specialFund","specialFund");
		} else if("CM".equals(_node)){
			//三级,类别树采煤节点
			childNodes = this.CMList();
		} else if("JJ".equals(_node)){
			//三级,类别树掘进节点
			childNodes = this.JJList();
		} else if("TF".equals(_node)){
			//三级,类别树通防节点
			childNodes = this.TFList();
		} else if("QT".equals(_node)){
			//三级,类别树其他节点
			childNodes = this.QTList();
		}
		else {
			if("topFaceRoadway".equals(tableName)) {
				//二级, 工作面巷道树根节点下的一级节点
				childNodes = EngineeringDesign.getNodeToProjectInfo(_node);
			}
			else {
				if("1".equals(tableName)){
					//三级, 工作面巷道树根节点下的一级节点“工作面”的叶子节点	
					childNodes = WorkingFace.getLeafFromWorkingFace(_node);
				} else if("2".equals(tableName)){
					//三级, 工作面巷道树根节点下的一级节点“巷道”的叶子节点	
					childNodes = RoadWay.getLeafFromRoadWay(_node);
				} else {
					//三级, 工作面巷道树根节点下的一级节点“其他”的叶子节点	
					childNodes = OtherDesign.getLeafFromOtherDesign(_node);
				}
			}
		}
		return childNodes;
	}
	
	/**
	 * 
	 * <p>Function: initMetaData</p>
	 * <p>Description: 定义树中block的列</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午04:06:37 张小飞
	 * </p>
	 *
	 */
	public EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();

		EiColumn eiColumn = new EiColumn("value");
		eiColumn.setDescName("value");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("display");
		eiColumn.setDescName("display");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("tableName");
		eiColumn.setDescName("tableName");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);

		eiColumn = new EiColumn("leaf");
		eiColumn.setDescName("leaf");
		eiColumn.setType(EiConstant.COLUMN_TYPE_NUMBER);
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		return eiMetadata;
	}
	
	/**
	 * 
	 * <p>Function: getTreeNodesFromInitialData</p>
	 * <p>Description: 将block重新构造，返回含有树中的列的List</p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午04:10:02 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List<Map> getTreeNodesFromInitialData(String blockName, String tableName){
		List<Map> nodes = new ArrayList<Map>();
		EiBlock block = InitialData.getInstance().getEiBlockByNo(blockName);
		List<Map> rowList = block.getRows();
		for(Map map : rowList){
			Set<String> keys = map.keySet();
			String value = "";
			String display = "";
			for(String key : keys){
				if("value".equals(key)){
					value = (String)map.get("value");
				} else if("display".equals(key)){
					display = (String)map.get("display");
				}
			}
			
			Map<String, String> _map = new HashMap<String, String>();
			_map.put("value",value);
			_map.put("display",display);
			_map.put("leaf", "1");
			_map.put("tableName", tableName);
			nodes.add(_map);
		}	
		return nodes;
	}
	
	/**
	 * 工程类型
	 * <p>Function: getTreeNodesFromInitialData</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-23上午09:44:58 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes"})
	private List<Map> getTreeNodesFromInitialData(){
		List<Map> _topNodes = new ArrayList<Map>();

		Map<String, String> _tempMap = new HashMap<String, String>();
		_tempMap.put("value", "CM");
		_tempMap.put("display", "采煤");
		_tempMap.put("tableName", "category");
		_tempMap.put("leaf", "0");
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "JJ");
		_tempMap.put("display", "掘进");
		_tempMap.put("tableName", "category");
		_tempMap.put("leaf", "0");
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "TF");
		_tempMap.put("display", "通防");
		_tempMap.put("tableName", "category");
		_tempMap.put("leaf", "0");
		_topNodes.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "QT");
		_tempMap.put("display", "其他");
		_tempMap.put("tableName", "category");
		_tempMap.put("leaf", "0");
		_topNodes.add(_tempMap);
		
		return _topNodes;
	}

	/**
	 * 采煤的二级分类
	 * <p>Function: CMList</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map<String,String>></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-1上午08:15:23 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private List<Map> CMList() {
		List<Map> _list = new ArrayList<Map>();
		
		Map<String, String> _tempMap = new HashMap<String, String>();
		_tempMap.put("value", "综采");
		_tempMap.put("display", "综采");
		_tempMap.put("tableName", "CM");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "连采");
		_tempMap.put("display", "连采");
		_tempMap.put("tableName", "CM");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "机采");
		_tempMap.put("display", "机采");
		_tempMap.put("tableName", "CM");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "炮采");
		_tempMap.put("display", "炮采");
		_tempMap.put("tableName", "CM");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		return _list;
	}
	
	/**
	 * 掘进的二级分类
	 * <p>Function: CMList</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map<String,String>></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-1上午08:15:23 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private List<Map> JJList() {
		List<Map> _list = new ArrayList<Map>();
		
		Map<String, String> _tempMap = new HashMap<String, String>();
		_tempMap.put("value", "开拓");
		_tempMap.put("display", "开拓");
		_tempMap.put("tableName", "JJ");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "回采");
		_tempMap.put("display", "回采");
		_tempMap.put("tableName", "JJ");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		return _list;
	}
	
	/**
	 * 通防的二级分类
	 * <p>Function: CMList</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map<String,String>></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-1上午08:15:23 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private List<Map> TFList() {
		List<Map> _list = new ArrayList<Map>();
		
		Map<String, String> _tempMap = new HashMap<String, String>();
		_tempMap.put("value", "打钻");
		_tempMap.put("display", "打钻");
		_tempMap.put("tableName", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "抽采");
		_tempMap.put("display", "抽采");
		_tempMap.put("tableName", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "管路安装");
		_tempMap.put("display", "管路安装");
		_tempMap.put("tableName", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "水力压裂");
		_tempMap.put("display", "水力压裂");
		_tempMap.put("tableName", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		return _list;
	}
	
	
	/**
	 * 其他的二级分类
	 * <p>Function: CMList</p>
	 * <p>Description: </p>
	 * <p>return value:List<Map<String,String>></p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-9-1上午08:15:23 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private List<Map> QTList() {
		List<Map> _list = new ArrayList<Map>();
		
		Map<String, String> _tempMap = new HashMap<String, String>();
		_tempMap.put("value", "巷修");
		_tempMap.put("display", "巷修");
		_tempMap.put("childTypes", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "按拆");
		_tempMap.put("display", "按拆");
		_tempMap.put("childTypes", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "零星");
		_tempMap.put("display", "零星");
		_tempMap.put("childTypes", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "喷浆");
		_tempMap.put("display", "喷浆");
		_tempMap.put("childTypes", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "卧底");
		_tempMap.put("display", "卧底");
		_tempMap.put("childTypes", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		_tempMap = new HashMap<String, String>();
		_tempMap.put("value", "其他");
		_tempMap.put("display", "其他");
		_tempMap.put("childTypes", "TF");
		_tempMap.put("leaf", "1");
		_list.add(_tempMap);
		
		return _list;
	}
}
